﻿using System;

using System.IO;

using System.Net;

using System.ComponentModel;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Media;

using System.Windows.Media.Animation;

using System.Windows.Navigation;



namespace Sample

{

    public partial class NumericControl : INotifyPropertyChanged

    {

        private double numericValue = 0;



        public NumericControl()

        {

            this.InitializeComponent();
            Tip1.Text= "描述标题";
            Tip2.Text= "描述信息";
        }

       // double f;
        //      public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
        //"Values", typeof(int), typeof(NumericControl));
        //public double Values
        //{
        //    get { return (double)GetValue(StartValueProperty); }
        //    set { SetValue(StartValueProperty, value); }
        //}
        public static readonly DependencyProperty StartValueProperty =
    DependencyProperty.Register("Value", typeof(double), typeof(NumericControl));
        //      public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding)
        //      {
        //          return BindingOperations.SetBinding(this, dp, binding);
        //      }




        public double Value

        {

            //get { return numericValue; }

            //set

            //{

            //    numericValue = value;
            //    this.MaxHeight = value;
            //    NotifyPropertyChanged("Value");

            //}
            get { return (double)GetValue(StartValueProperty); }
            set { SetValue(StartValueProperty, value);
                //this.MaxHeight = value;
                NotifyPropertyChanged("Value");
            }
        }
        public double Increment { get; set; }
        public double MaxValue { get; set; }
        public double MinValue { get; set; }
        public string DescribeTitle { set { Tip1.Text = value; } }
        public string Describe {  set { Tip2.Text = value; } }
        public string DescribeLabels { set { Labels.Text = value; } }

        private void UpButton_Click(object sender, RoutedEventArgs e)

        {

            double newValue = (Value + Increment);

            if (newValue > MaxValue)

            {

                Value = MaxValue;

            }

            else

            {

                Value = newValue;

            }

        }



        private void DownButton_Click(object sender, RoutedEventArgs e)

        {

            double newValue = (Value - Increment);

            if (newValue < MinValue)

            {

                Value = MinValue;

            }

            else

            {

                Value = newValue;

            }

        }



        private void ValueText_LostFocus(object sender, RoutedEventArgs e)

        {

            try

            {

                Value = double.Parse(ValueText.Text);
                if (Value > MaxValue)

                {

                    Value = MaxValue;

                }
                if (Value < MinValue)

                {

                    Value = MinValue;

                }

            }

            catch (Exception)

            {

                Value = 0;

            }

        }





        #region INotifyPropertyChanged Members



        public event PropertyChangedEventHandler PropertyChanged;



        public void NotifyPropertyChanged(string propertyName)

        {

            if (PropertyChanged != null)

            {

                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

            }

        }



        #endregion

    }



    [ValueConversion(typeof(double), typeof(string))]

    public class DoubleValueConverter : IValueConverter

    {

        #region IValueConverter Members



        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            try

            {

                return value.ToString();

            }

            catch (Exception)

            {

                return "0";

            }

        }



        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            try

            {

                return double.Parse((string)value);

            }

            catch (Exception)

            {

                return 0;

            }

        }



        #endregion

    }

}